package top.kuxiao.crmserver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.kuxiao.crmserver.pojo.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaozhang
 */
@Service
public class StatementService {

    @Autowired
    ICustomerService customerService;

    @Autowired
    IOrdersService ordersService;

    @Autowired
    ICustomerLossService customerLossService;


    @Autowired
    IAccountManagerService accountManagerService;

    @Autowired
    IServicesService servicesService;

    /**
     * 根据年份分组
     *
     * @return map年份分组的统计
     */
    public List<Map<String, Object>> countAll(Integer id, String date) {


        List<Map<String, Object>> maps ;

        if (date!=null){
            maps=countAllDate(date,null);
        }else {
            maps = countAllId(id,null);
        }

        for (Map<String, Object> map : maps) {

            List<Map<String, Object>> maps1;

            if (date==null){
                maps1=countAllDate(date,map);
                map.put("year", maps1);
            }else {
                maps1 = countAllId(id,map);
                map.put("orders",maps1);
            }



        }




        return maps;
    }

    /**
     * 根据年份查询
     * */
    private List<Map<String, Object>> countAllDate(String date,Map<String, Object> map) {
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.select("DATE_FORMAT(orders_date,'%Y') as date, sum(orders_amount) sum");
        ordersQueryWrapper.groupBy("DATE_FORMAT(orders_date,'%Y')");


        if (date != null) {
            ordersQueryWrapper.eq("DATE_FORMAT(orders_date,'%Y')", date);
        }else if (map!=null){
            ordersQueryWrapper.eq("customer_id", map.get("customer_id"));
        }

        return ordersService.listMaps(ordersQueryWrapper);
    }

    /**
     * 根据客户分组
     *
     * @return map用户分组的统计
     */
    private List<Map<String, Object>> countAllId(Integer id,Map<String, Object> map) {
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();


        ordersQueryWrapper.select("customer_id,sum(orders_amount) sum");
        ordersQueryWrapper.groupBy("customer_id");


        customerQueryWrapper.select("customer_id,customer_name");
        customerQueryWrapper.groupBy("customer_id");


        if (id != 0) {
            ordersQueryWrapper.eq("customer_id", id);
            customerQueryWrapper.eq("customer_id", id);
        }else if (map!=null) {
            ordersQueryWrapper.eq("DATE_FORMAT(orders_date,'%Y')",map.get("date"));
        }

        List<Map<String, Object>> maps = customerService.listMaps(customerQueryWrapper);


        List<Map<String, Object>> map1 = ordersService.listMaps(ordersQueryWrapper);

        for (Map<String, Object> stringObjectMap : map1) {
            for (Map<String, Object> objectMap : maps) {
                if (stringObjectMap.get("customer_id") == objectMap.get("customer_id")) {
                    stringObjectMap.put("customer", objectMap);
                }
            }
        }

        System.out.println(map);

        return map1;
    }



    public List<Map<String, Object>> findCountAmId(String index){

        QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
        String column="customer_grade";

        if ("信用度".equals(index)){
            column="customer_credit_line";
        }
        if ("满意度".equals(index)){
            column="customer_satisfaction";
        }

        customerQueryWrapper.select(column+", count(customer_id) as count");
        customerQueryWrapper.groupBy(column);

        return customerService.listMaps(customerQueryWrapper);

    }



    public List<Map<String, Object>> loss(Integer id){

        QueryWrapper<CustomerLoss> customerLossQueryWrapper = new QueryWrapper<>();
        QueryWrapper<AccountManager> accountManagerQueryWrapper = new QueryWrapper<>();

        customerLossQueryWrapper.select("account_manager_id,count(customer_id) as count");
        customerLossQueryWrapper.eq("customer_loss_state","流失");
        customerLossQueryWrapper.groupBy("account_manager_id");

        List<Map<String, Object>> map1=new ArrayList<>();

        accountManagerQueryWrapper.select("am_name,am_realname");



        if (id!=0){
            //TODO 应该显示满意度等的计数,要先连表
            customerLossQueryWrapper.eq("account_manager_id",id);
            accountManagerQueryWrapper.eq("am_id",id);
            map1 = customerLossService.listMaps(customerLossQueryWrapper);
            if (map1.size()==0){
                return null;
            }
            Map<String, Object> map = accountManagerService.getMap(accountManagerQueryWrapper);
            map1.get(0).put("AM",map);


            List<List<Map<String, Object>>> map3=new ArrayList<>();
            for (int i=0;i<3;i++){
                QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
                customerQueryWrapper.eq("customer_cid",id).eq("customer_state","流失");

                String column="customer_grade";

                String index="lv";

                if (i==1){
                    index="credit";
                    column="customer_credit_line";
                }
                if (i==2){
                    index="satisfaction";
                    column="customer_satisfaction";
                }

                customerQueryWrapper.select(column+" as "+index+", count(customer_id) as count");
                customerQueryWrapper.groupBy(column);
                List<Map<String, Object>> map2 = customerService.listMaps(customerQueryWrapper);
                map3.add(map2);

            }
            map1.get(0).put("score",map3);
        }else {
            map1 = customerLossService.listMaps(customerLossQueryWrapper);

            for (Map<String, Object> stringObjectMap : map1) {
                QueryWrapper<AccountManager> accountManagerQueryWrapper1 = new QueryWrapper<>();
                accountManagerQueryWrapper1.select("am_name,am_realname");
                accountManagerQueryWrapper1.eq("am_id",stringObjectMap.get("account_manager_id").toString());
                Map<String, Object> map = accountManagerService.getMap(accountManagerQueryWrapper1);

                stringObjectMap.put("AM",map);
            }
        }
        return map1;

    }


    public List<Object> servicesCount(String year){

        List<Object> list=new ArrayList<>();

         // 满意度统计

        QueryWrapper<Services> servicesQueryWrapper = new QueryWrapper<>();
        servicesQueryWrapper.select("services_satisfaction,count(customer_id)");
        servicesQueryWrapper.eq("DATE_FORMAT(services_allocate_time,'%Y')",year);
        servicesQueryWrapper.eq("services_state","已归档");
        servicesQueryWrapper.groupBy("services_satisfaction");

        List<Map<String, Object>> map1 = servicesService.listMaps(servicesQueryWrapper);

        //  类型统计
        QueryWrapper<Services> servicesQueryWrapper1 = new QueryWrapper<>();
        servicesQueryWrapper1.select("services_type,count(customer_id)");
        servicesQueryWrapper1.eq("DATE_FORMAT(services_allocate_time,'%Y')",year);
        servicesQueryWrapper1.eq("services_state","已归档");
        servicesQueryWrapper1.groupBy("services_type");

        List<Map<String, Object>> map2 = servicesService.listMaps(servicesQueryWrapper1);

        list.add(map1);
        list.add(map2);
        return list;

    }

}
